博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js单图片上传
阅读量:4354 次
发布时间:2019-06-07

本文共 2724 字,大约阅读时间需要 9 分钟。

 

//这里一定要写name 后台就是通过name接受内容
{if condition="($company_info.cover_img)"}
{else/}
{/if}

 

前台js

 

 
$('#file').change(function(){        if (this.files && this.files[0]) {            var objUrl = getObjectURL(this.files[0]);            console.log(objUrl);            var data = new FormData($('#form1')[0]);            console.log($('#form1'));            console.log(data);            $.ajax({                 url: "{:url('advert/upload_img')}",                 type: 'POST',                 data: data,                async: false,                dataType: 'JSON',                 processData: false,                 contentType: false,            }).done(function(ret){             // alert(ret.path_img);                if(ret.error==1){                    $('#cover_img').attr('src',ret.path_img);                    $('#cover_img').css('width','100px');                    $('#cover_img').css('height','100px');                }else{                    alert('上传失败');                }            });         }    })

 

 

 

 
function getObjectURL(file) {                       var url = null;            if (window.createObjectURL != undefined) { // basic                url = window.createObjectURL(file);            } else if (window.URL != undefined) { // mozilla(firefox)                url = window.URL.createObjectURL(file);            } else if (window.webkitURL != undefined) { // webkit or chrome                url = window.webkitURL.createObjectURL(file);            }            return url;    }
 

 

 

 

 

后台php

public function upload(){        $file = request()->file('head_img');        $path=str_replace('\\', '/', 'upload/box');        if (!is_dir($path)) {            mkdir($path, 0777,true);        }        // $path=company_coverimg_path('854');        if($file){            $info = $file->move($path);            if($info){                $path_img ='/'.$path.'/'.$info->getSaveName();                 $data['path_img']=$path_img;                $data['error']=1;            }else{                // 上传失败获取错误信息                $msg = $file->getError();                $data['error']=2;                $data['msg']=$msg;            }        }        echo json_encode($data);    }

 

 

 

 

 

 

<scroll-view scroll-y="true" style="height: 200rpx;">

<view style="background: red; width: 200px; height: 100px; display: inline-block" ></view>
<view style="background: green; width: 200px; height: 100px; display: inline-block"></view>
<view style="background: blue; width: 200px; height: 100px; display: inline-block"></view>
<view style="background: yellow; width: 200px; height: 100px; display: inline-block"></view>
</scroll-view>

转载于:https://www.cnblogs.com/chentailin/p/8991460.html

你可能感兴趣的文章
Xmind?
查看>>
spring+quartz 实现定时任务三
查看>>
day2-三级菜单
查看>>
linux下升级4.5.1版本gcc
查看>>
Beanutils
查看>>
FastJson
查看>>
excel4j
查看>>
Thread
查看>>
HtmlEmail
查看>>
ThreadLocal
查看>>
线程池
查看>>
XMAL 中x名称控件的Auttribute
查看>>
java笔记11-内部类
查看>>
基本数据类型
查看>>
BZOJ 1004 [HNOI2008]Cards
查看>>
[POJ 2689] Prime Distance
查看>>
[ 原创 ] Linux下查找指定类型文件以及删除
查看>>
win10环境下jdk1.8+Android Developer Tools Build: v22.3.0-887826的问题
查看>>
辗转相除法求最大公约数
查看>>
Centos7 安装mysql5.7.16
查看>>